using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;


namespace CPRMulti.Tools
{
    public class UIAction : MonoBehaviour
    {

        Dictionary<string, System.Action> actionlist = new Dictionary<string, System.Action>();
        List<string> deletekeys = new List<string>();

        static UIAction _uication = null;

        public static UIAction Get()
        {
            if (_uication == null)
            {
                GameObject obj = new GameObject("actionlist");
                _uication = obj.AddComponent<UIAction>();
            }
            return _uication;
        }

        public void Add(string key, System.Action action)
        {
            if (!actionlist.ContainsKey(key))
                actionlist.Add(key, action);
        }

        public void Remove(string key)
        {
            if (!actionlist.ContainsKey(key))
                return;
            deletekeys.Add(key);
        }

        void Update()
        {
            foreach (KeyValuePair<string, System.Action> pair in actionlist)
            {
                pair.Value.Invoke();
            }
        }

        void LateUpdate()
        {
            for (int i = 0; i < deletekeys.Count; i++)
            {
                actionlist[deletekeys[i]] = null;
                actionlist.Remove(deletekeys[i]);
            }
            deletekeys.Clear();
        }

    }
}